<template>
    <div>
        <div>
            <h4 class="font-weight-bold">客户列表</h4>
        </div>

        <!-- 执行打分卡和预警规则检测Modal -->
        <Modal 
            :closable="false"
            :mask-closable="false"
            v-model="RunDetectModal.show"
            :title="'【' + RunDetectModal.choiseCustomer.customerName + '】执行操作'">
                <!-- 执行进度 -->
                <div style="height:74px;" class="mb-4">
                    <b-alert show dismissible variant="info" v-show="RunDetectModal.progress.rercent !== 100">
                        <span v-show="!RunDetectModal.progress.show">提示：请勾选执行打分卡或者预警规则！</span>
                        <span v-show="RunDetectModal.progress.show">{{RunDetectModal.progress.total}} 项检测执行中...请稍后</span>
                    </b-alert>
                    <b-alert show dismissible variant="info" v-show="RunDetectModal.progress.status === 'success'">
                        <span >
                            提示：{{RunDetectModal.progress.total}} 项检测已执行完毕！<a  href="javascript:void(0);" class="ml-2 text-link" @click="goDetail">点击查看执行日志</a>
                        </span>
                    </b-alert>
                    <Progress 
                        v-if="RunDetectModal.progress.show"
                        :percent="RunDetectModal.progress.rercent" 
                        :status="RunDetectModal.progress.status"></Progress>
                </div>

                <div class="p-card mb-4">
                    <h5 class="card-title">打分卡</h5>
                    <div class="card-container">
                        <p class="card-item" 
                            v-if="RunDetectModal.score.optionList.length > 0" 
                            v-for="(item) in RunDetectModal.score.optionList" 
                            :key="item.oid">

                            <CheckboxGroup class="card-item-checkbox"
                                v-model="RunDetectModal.score.selectList">
                                <Checkbox style="display:block;" 
                                    :disabled="RunDetectModal.dirty"
                                    :key="item.oid" 
                                    :label="item.oid" >
                                    <span>{{item.name}}</span>
                                </Checkbox>
                            </CheckboxGroup>
                            <span class="item-status" v-show="item.state !== 'default'">
                                <Icon v-show="item.state === 'waiting'" :title="item.desc" type="load-c" size=18 class="demo-spin-icon-load text-primary"/>
                                <Icon v-show="item.state === 'success'" :title="item.desc" type="checkmark-circled" class="text-success"/>
                                <Icon v-show="item.state === 'error'" :title="item.desc" type="alert-circled" class="text-danger"/>
                            </span>
                        </p>
                        <p v-else class="text-muted small text-center">
                            暂无数据
                        </p>
                    </div>
                </div>

                 <div class="p-card">
                    <h5 class="card-title">预警规则</h5>
                    <div class="card-container">
                        <p class="card-item"
                            v-if="RunDetectModal.alertRule.optionList.length > 0"
                            v-for="(item) in RunDetectModal.alertRule.optionList" 
                            :key="item.oid">
                            
                            <CheckboxGroup v-model="RunDetectModal.alertRule.selectList">
                                <Checkbox class="card-item-checkbox" style="display:block;" 
                                    :disabled="RunDetectModal.dirty"
                                    :key="item.oid" 
                                    :label="item.oid" >
                                    <span>{{item.name}}</span>
                                </Checkbox>
                            </CheckboxGroup>
                            <span class="item-status" v-show="item.state !== 'default'">
                                <Icon v-show="item.state === 'waiting'" :title="item.desc" type="load-c" size=18 class="demo-spin-icon-load text-primary"/>
                                <Icon v-show="item.state === 'success'" :title="item.desc" type="checkmark-circled" class="text-success"/>
                                <Icon v-show="item.state === 'error'" :title="item.desc" type="alert-circled" class="text-danger"/>
                            </span>
                        </p>
                        <p v-else class="text-muted small text-center">
                            暂无数据
                        </p>
                    </div>
                </div>

                <br />
                <div slot="footer">
                    <Button 
                        type="ghost" class="mr-2" icon="loop"
                        :disabled="!RunDetectModal.dirty"
                        @click="() => {
                            let self = this;
                            RunDetectModal.clearFn.call(self);
                        }">重新选择</Button>
                    <Button 
                        type="primary" class="mr-2" icon="arrow-right-b"
                        @click="() => {
                            let self = this;
                            RunDetectModal.executeFn.call(self);
                        }" 
                        :disabled="RunDetectModal.dirty">执行</Button>
                    <Button type="ghost" icon="close-circled"
                        @click="() => {
                            let self = this;
                            RunDetectModal.cancelFn.call(self);
                        }">关闭</Button>
                </div>
        </Modal>

        <!-- 临时客户切换 -->
        <Modal 
            :closable="false"
            :mask-closable="false"
            v-model="TransformCompanyPanel.show" 
            title="切换临时客户">
            
            <Form 
                :model="TransformCompanyPanel.form" 
                label-position="right"  
                :label-width="120">

                <FormItem label="选择金融服务：">
                    <RadioGroup vertical
                        v-model="TransformCompanyPanel.form.serviceCode">
                        <Radio 
                            v-for="serviceItem in serviceList" 
                            :key="serviceItem.code" 
                            :disabled="serviceItem.udcValue === 'temp'"
                            :label="serviceItem.code">{{serviceItem.name}}</Radio>
                    </RadioGroup>
                </FormItem>
            </Form>

            <div slot="footer">
                <Button 
                    type="primary" class="mr-2" icon="checkmark-round"
                    @click="() => {
                        let self = this;
                        TransformCompanyPanel.confirmFn.call(self);
                    }"
                    :loading="TransformCompanyPanel.loading">确定</Button>
                <Button type="ghost" icon="close-circled"
                    @click="() => {
                        let self = this;
                        TransformCompanyPanel.cancelFn.call(self);
                    }">取消</Button>
            </div>
        </Modal>

        <!-- 新增企业客户 -->
        <Modal
            :closable="false"
            :mask-closable="false"
            v-model="addCompanyPanel.show"
            title="新增企业客户"
            :loading="addCompanyPanel.loading">

            <Form :model="addCompanyPanel.form" label-position="right"  :label-width="120">
                <FormItem label="企业全称">
                    <i-input v-model="addCompanyPanel.form.customerName" placeholder="请输入企业全称"/>
                </FormItem>
                <FormItem label="统一社会信用代码">
                    <i-input v-model="addCompanyPanel.form.taxpayerIdentityNumber" placeholder="请输入企业统一社会信用代码"/>
                </FormItem>
                <FormItem label="企业法人">
                    <i-input v-model="addCompanyPanel.form.legalPerson" placeholder="请输入企业法人"/>
                </FormItem>
                <FormItem label="注册资金">
                    <i-input v-model="addCompanyPanel.form.registeredCapital" placeholder="请输入企业注册资金"/>
                </FormItem>
                <FormItem label="注册时间">
                    <DatePicker type="date" format="yyyy-MM-dd HH:mm:ss" placeholder="请输入企业注册时间" style="width: 200px" @on-change=" (date) => addCompanyPanel.form.establishedTime = date"></DatePicker>
                </FormItem>
            </Form>
            <div slot="footer">
                <Button 
                    type="primary" class="mr-2" 
                    @click="() => {
                        let self = this;
                        addCompanyPanel.confirm.call(self);
                    }" 
                    :loading="addCompanyPanel.loading">确定</Button>
                <Button type="ghost"  
                    @click="() => {
                        let self = this;
                        addCompanyPanel.cancel.call(self);
                    }">取消</Button>
            </div>
        </Modal>


        <b-card class="mb-4">
            <div class="card-content">
                <RadioGroup v-model="choiseService" type="button" @on-change="switchService">
                    <Radio v-for="serviceItem in serviceList" :key="serviceItem.code" :label="serviceItem.code">{{serviceItem.name}}</Radio>
                </RadioGroup>

                <RadioGroup class="ml-2" v-model="choiseService" type="button" @on-change="switchService">
                    <Radio label="temp">临时企业客户</Radio>
                </RadioGroup>
                <Button type="primary" :disabled="choiseService != 'temp'" @click="addCompanyPanel.show = true" shape="circle" title="手动添加客户" size="small"><Icon type="plus-round" class
                     ="mr-2"></Icon>手动录入</Button>

                <Table :data="table.data" :columns="table.columns" :loading="table.loading" stripe border class="mt-2"></Table>
                <Page show-elevator show-total size="small" class="mt-2" :total="table.total" :current="table.currentPage" @on-change="pageChange"></Page>
            </div>
        </b-card>
        
    </div>
</template>

<style src="@/vendor/libs/sweet-modal-vue/sweet-modal-vue.scss" lang="scss"></style>
<style scoped>
.ivu-form-item{
    margin-bottom: 8px !important;
}

.p-card{
    border: 1px solid #eaeaea;
}
h5.card-title{
    height: 38px;
    line-height: 38px;
    padding-left: 12px;
    color:#4E8CE8;
    font-weight: bold;
    margin-bottom: 0px;
    border-bottom: 1px solid #eaeaea;
}
.card-container{
    min-height:40px;
    max-height:150px;
    overflow-y:auto;
}

p.card-item{
    padding-left: 14px;
    margin-bottom: 0px;
    line-height: 28px;
    position:relative;
}

span.item-status{
    position:absolute;
    right: 20px;
    top: 0;
}

p.card-item label{
    margin-bottom:0px;
}

p.card-item:nth-child(odd){
    background-color: #ECF3FC;
}
</style>

<script>
import Vue from "vue";
import Axios from "axios";
import expandRow from "./table-expand.vue";

function padLeftZero(str) {
    return ("00" + str).substr(str.length);
}

function formatDate(longStr, fmt) {
    let date = new Date(longStr);

    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(
            RegExp.$1,
            (date.getFullYear() + "").substr(4 - RegExp.$1.length)
        );
    }
    let o = {
        "M+": date.getMonth() + 1,
        "d+": date.getDate(),
        "h+": date.getHours(),
        "m+": date.getMinutes(),
        "s+": date.getSeconds()
    };
    for (let k in o) {
        if (new RegExp(`(${k})`).test(fmt)) {
            let str = o[k] + "";
            fmt = fmt.replace(
                RegExp.$1,
                RegExp.$1.length === 1 ? str : padLeftZero(str)
            );
        }
    }
    return fmt;
}

export default {
    name: "tables-vue-tables-2",
    metaInfo: {
        title: "企业列表 - 智能风控平台"
    },
    data() {
        return {
            // 客户切换
            TransformCompanyPanel: {
                show: false,
                loading: false,
                form: {
                    serviceCode: 'jinrongbaoli',
                    customerOid: null
                },
                // 确认方法
                confirmFn: function(){
                    let self = this;
                    let panel = self.TransformCompanyPanel;
                    panel.loading = true;
                    self.transformReq().then(function(res){
                        if(res.code === 0){
                            self.$Message.info('切换成功');
                            panel.show = false;
                            // 重新刷新
                            self.table.data = [];
                            self.loadCustomerList().then(function(res){
                                if(res.data){
                                    self.table.data = res.data.data ? res.data.data : [];
                                }
                            });
                        }else{
                            self.$Message.error(res.desc);
                        }
                        panel.loading = false;
                    },function(res){
                        self.$Message.error(res.desc);
                        panel.loading = false;
                    });
                },
                // 取消方法
                cancelFn: function(){
                    let self = this;
                    let panel = self.TransformCompanyPanel;
                    panel.form = {
                        serviceCode: 'jinrongbaoli',
                        customerOid: null
                    };
                    panel.show = false;
                }
            },
            // 新增临时客户
            addCompanyPanel: {
                // 是否显示
                show: false,
                // 表单请求中
                loading: false,
                // 表单数据
                form: {
                    customerName: '',   //企业全称
                    taxpayerIdentityNumber: '',//统一社会信用代码
                    legalPerson: '',    //企业法人
                    registeredCapital: '',  //注册资金
                    establishedTime: ''   //注册时间
                },

                // 确认方法
                confirm: function(){
                    let self = this;
                    self.addCompanyPanel.loading = true;
                    self.addCustomerReq().then(function(resp){
                        self.addCompanyPanel.loading = false;

                        if(resp.code === 0){
                            self.addCompanyPanel.show = false;
                            self.addCompanyPanel.form = {
                                customerName: '',
                                taxpayerIdentityNumber: '',
                                legalPerson: '',
                                registeredCapital: ''
                            }
                            self.$Message.info('添加成功！');
                            self.pageChange(1);
                        }else{
                            self.$Message.error('添加失败！');
                        }
                    }).catch(function(resp){
                        self.addCompanyPanel.loading = false;
                        self.$Message.error('添加失败！');
                    });
                    // console.log(self.addCompanyPanel);
                },
                cancel: function(){
                    let self = this;
                    self.addCompanyPanel.show = false;
                },
            },
            // 打分卡+预警规则检测弹框
            RunDetectModal: {
                // 是否显示
                show: false,
                // 是否已执行过
                dirty: false,
                // 当前选中的客户
                choiseCustomer: {
                    oid: null,
                    customerName: null
                },
                // 检测进度
                progress: {
                    total: 0,   // 执行总数
                    successNum: 0,  // 执行成功数量
                    rercent: 0,  // 执行进度百分比
                    status: 'active', // active/wrong/success
                    show: false // 是否显示执行进度
                },
                // 打分卡对象
                score: {
                    optionList: [],
                    selectList: []
                },
                // 预警规则对象
                alertRule: {
                    optionList: [],
                    selectList: []
                },
                // 执行
                executeFn: function(){
                    let modal = this.RunDetectModal;
                    let progress = modal.progress;
                    let scoreObj = modal.score;
                    let alertRuleObj = modal.alertRule;

                    // 执行总数
                    progress.total = scoreObj.selectList.length + alertRuleObj.selectList.length;
                    if(progress.total > 0){
                        // 执行完成百分比
                        progress.rercent = parseInt(new Number((progress.successNum / progress.total) * 100).toFixed(2));
                        // 显示执行进度
                        progress.show = true;

                        modal.dirty = true;
                        this.runTool();
                    }
                },
                // 关闭
                cancelFn: function(){
                    let modal = this.RunDetectModal;
                    modal.clearFn.call(this);
                    modal.score.optionList = [];
                    modal.alertRule.optionList = [];
                    // 隐藏弹框
                    modal.show = false;
                },
                // 重置
                clearFn: function(){
                    let modal = this.RunDetectModal;
                    
                    modal.dirty = false;

                    modal.progress= {
                        total: 0,   // 执行总数
                        successNum: 0,
                        rercent: 0,  // 执行进度百分比
                        status: 'active', // active/wrong/success
                        show: false, // 是否显示执行进度
                        
                    };

                    modal.score.selectList= [];
                    modal.score.optionList.forEach(function(item){
                        item.state = 'default';
                    });

                    modal.alertRule.selectList= [];
                    modal.alertRule.optionList.forEach(function(item){
                        item.state = 'default';
                    });
                },
                // 进度推进
                ticktock: function(){
                    let progress = this.RunDetectModal.progress;
                    // 改变进度情况
                    progress.successNum++;
                    progress.rercent = parseInt(new Number((progress.successNum / progress.total) * 100).toFixed(2));
                    if(progress.successNum === progress.total){
                        progress.status = 'success';
                    }
                }
            },
            // 金融服务列表
            serviceList: [],
            // 当前默认的金融服务
            choiseService: 'jinrongbaoli',
            // 客户表格数据对象
            table: {
                // 表格数据
                data: [],
                // 表格列配置
                columns: [
                    {
                        title: "客户企业名称",
                        width: 220,
                        key: "customerName"
                    },
                    {
                        title: "纳税人识别号",
                        key: "name"
                    },
                    {
                        title: "法定代表人",
                        key: "legalPerson",
                        align: "center"
                    },
                    {
                        title: "法人身份证",
                        key: "name"
                    },
                    {
                        title: "成立时间",
                        key: "establishedTime",
                        align: "center",
                        render: (h, params) => {
                            // console.log(params);
                            let fmtDate = formatDate(
                                params.row.establishedTime,
                                "yyyy-MM-dd"
                            );
                            return <div>{fmtDate}</div>;
                        }
                    },
                    {
                        title: "是否关注",
                        key: "watch",
                        render: (h, context) => {
                            let watch = "未关注";
                            if (context.row.watch) {
                                return (
                                    <i-button
                                        type="primary"
                                        size="small"
                                        class="mr-2"
                                        shape="circle"
                                        title="已关注"
                                        icon="ios-heart"
                                        onClick={() => {
                                            this.$Modal.confirm({
                                                title: "系统提示",
                                                content: "<p>确认取消关注“" + context.row.customerName + "”？</p>",
                                                onOk: () => {
                                                    let self = this;
                                                    self.followReq(false,context.row.oid).then(function(res){
                                                        context.row.watch = false;
                                                        self.$Message.success('取消关注成功！');
                                                    });
                                                }
                                            });
                                        }}
                                    >
                                    已关注
                                    </i-button>
                                )
                            }else{
                                return (
                                    <i-button
                                        type="ghost"
                                        size="small"
                                        class="mr-2"
                                        shape="circle"
                                        title="未关注"
                                        icon="ios-heart-outline"
                                        onClick={() => {
                                            let self = this;
                                            self.followReq(true,context.row.oid).then(function(res){
                                                context.row.watch = true;
                                            });
                                        }}
                                    >
                                    未关注
                                    </i-button>
                                )
                            }
                        }
                    },
                    {
                        title: "操作",
                        key: "name",
                        width: 240,
                        align: "center",
                        fixed: 'right',
                        render: (h, context) => {
                            let vm = this;
                            
                            if(vm.choiseService === 'temp'){
                                return (
                                    <div>
                                        <i-button
                                            type="text"
                                            size="small"
                                            class="mr-2"
                                            shape="circle"
                                            title="转换"
                                            icon="arrow-return-right"
                                            onClick={() => {
                                                this.TransformCompanyPanel.form.customerOid = context.row.oid;
                                                this.TransformCompanyPanel.show = true;
                                            }}
                                        />
                                        <i-button
                                            type="text"
                                            size="small"
                                            class="mr-2"
                                            shape="circle"
                                            title="历史"
                                            icon="clock"
                                            onClick={() => {

                                            }}
                                        />
                                        <i-button
                                            type="text"
                                            size="small"
                                            class="mr-2"
                                            shape="circle"
                                            title="跑分"
                                            icon="play"
                                            onClick={() => {
                                                // 赋值选中的客户
                                                vm.RunDetectModal.choiseCustomer =  context.row;
                                                // 显示弹框
                                                vm.RunDetectModal.show = true;
                                                // 查询数据
                                                vm.loadScoreCardList();
                                                vm.loadAlertRuleList();
                                            }}
                                        />
                                        <i-button
                                            type="text"
                                            size="small"
                                            class="mr-2"
                                            shape="circle"
                                            icon="eye"
                                            title="查看详情"
                                            onClick={() => {
                                                return vm.edit(context);
                                            }}
                                        />
                                        <i-button
                                            type="text"
                                            size="small"
                                            class="mr-2"
                                            shape="circle"
                                            icon="edit"
                                            title="编辑客户"
                                            onClick={() => {
                                                return vm.edit(context);
                                            }}
                                        />
                                        <i-button
                                            type="text"
                                            size="small"
                                            class="mr-2"
                                            shape="circle"
                                            title="跑分日志"
                                            icon="document-text"
                                            onClick={() => {
                                                return vm.viewResult(context);
                                            }}
                                        />
                                    </div>
                                ); 
                            } else {
                                return (
                                    <div>
                                        <i-button
                                            type="text"
                                            size="small"
                                            class="mr-2"
                                            shape="circle"
                                            title="历史"
                                            icon="clock"
                                            onClick={() => {
    
                                            }}
                                        />
                                        <i-button
                                            type="text"
                                            size="small"
                                            class="mr-2"
                                            shape="circle"
                                            title="跑分"
                                            icon="play"
                                            onClick={() => {
                                                // 赋值选中的客户
                                                vm.RunDetectModal.choiseCustomer =  context.row;
                                                // 显示弹框
                                                vm.RunDetectModal.show = true;
                                                // 查询数据
                                                vm.loadScoreCardList();
                                                vm.loadAlertRuleList();
                                            }}
                                        />
                                        <i-button
                                            type="text"
                                            size="small"
                                            class="mr-2"
                                            shape="circle"
                                            icon="eye"
                                            title="查看详情"
                                            onClick={() => {
                                                return vm.edit(context);
                                            }}
                                        />
                                        <i-button
                                            type="text"
                                            size="small"
                                            class="mr-2"
                                            shape="circle"
                                            icon="edit"
                                            title="编辑客户"
                                            onClick={() => {
                                                return vm.edit(context);
                                            }}
                                        />
                                        <i-button
                                            type="text"
                                            size="small"
                                            class="mr-2"
                                            shape="circle"
                                            title="跑分日志"
                                            icon="document-text"
                                            onClick={() => {
                                                return vm.viewResult(context);
                                            }}
                                        />
                                    </div>
                                );
                            }

                        }
                    }
                ],
                // 是否载入中
                loading: false,
                // 总页数
                total: 0,
                // 当前页数
                currentPage : 1,
                // 每页条目数
                pageSize : 10
            }
        };
    },
    filters: {
        formatDate(time) {
            var date = new Date(time);
            return formatDate(date, "yyyy-MM-dd");
        }
    },
    created() {
        let self = this;
    },
    mounted(){
        let self = this;

        self.serviceList = [];
        //初始化加载金融服务列表
        self.loadServiceList().then(function(res){
            //获取服务列表
            self.serviceList = res.data.data ? res.data.data : [];

            //加载客户列表
            self.table.data = [];
            self.loadCustomerList().then(function(res){
                // console.log(res);
                if(res.data){
                    self.table.data = res.data.data ? res.data.data : [];
                }
            });
        }).catch(function(error){
            self.$Message.error(error)
        });
    },
    methods: {
        goDetail(){
            this.$router.push({
                path: "/customer/toolResult",
                query: {
                    oid: this.RunDetectModal.choiseCustomer.oid,
                    customerName:this.RunDetectModal.choiseCustomer.customerName
                }
            });
        },
        followReq(doFollow,customerOid){
            let self = this;
            return new Promise(function(resolve,reject){
                self.$http
                .patch('/app/v1/customers/'+customerOid+'/attention?isAttention='+doFollow)
                    .then(function(res) {
                        resolve(res);
                    },function(res){
                        reject(res);
                    });
            });
        },
        transformReq(){
            let self = this;
            return new Promise(function(resolve,reject){
                self.$http
                .patch('/app/v1/customers/'+self.TransformCompanyPanel.form.customerOid+'/serviceSwitch?serviceCode='+self.TransformCompanyPanel.form.serviceCode)
                    .then(function(res) {
                        resolve(res);
                    },function(res){
                        reject(res);
                    });
            });
        },
        addCustomerReq(){
            let self = this;

            return new Promise(function(resolve,reject){
                self.$http
                    .post('/app/v1/customers',self.addCompanyPanel.form)
                    .then(function(res) {
                        resolve(res);
                    },function(res){
                        reject(res);
                    });
            });
        },
        pageChange(pageNum){
            let self = this;
            self.table.currentPage = pageNum;
            self.loadCustomerList().then(function(res){
                if(res.data){
                    self.table.data = res.data.data ? res.data.data : [];
                }
            });
        },
        switchService(){
            let self = this;
            self.loadCustomerList().then(function(res){
                if(res.data){
                    self.table.data = res.data.data ? res.data.data : [];
                }
            });
        },
        loadCustomerList(){
            let self = this;
            self.table.loading = true;

            return new Promise(function(resolve,reject){
                self.$http
                    .get('/app/v1/customers',{
                        params: {
                            pageSize: 10,
                            currentPage: self.table.currentPage,
                            serviceCode: self.choiseService
                        }
                    })
                    .then(function(res) {
                        self.table.total = res.data.recordCount;
                        self.table.loading = false;
                        // console.log(self.table.total);
                        resolve(res);
                    },function(res){
                        self.table.loading = false;
                        reject(res);
                    });
            });
        },
        loadServiceList(){
            let self = this;
            return new Promise(function(resolve,reject){
                self.$http
                    .get('/app/v1/riskServices')
                    .then(function(res){
                        // console.log(res);
                        resolve(res);
                    },function(res){
                        reject(res);
                    });
            })
        },
        edit: function(params) {
            let self = this;
            self.$router.push({
                path: "/customer/datadown",
                query: {
                    oid: params.row.oid
                }
            });
        },
        viewResult: function(params) {
            let self = this;
            // console.log(params);
            self.$router.push({
                path: "/customer/toolResult",
                query: {
                    oid: params.row.oid,
                    customerName:params.row.customerName
                }
            });
        },
        runTool: function(){
            let self = this;
            let modal = self.RunDetectModal;
            let customerOid = modal.choiseCustomer.oid;
            let progress = modal.progress;
            let scoreSelectList = modal.score.selectList;
            let scoreOptionList = modal.score.optionList;
            let alertRuleSelectList = modal.alertRule.selectList;
            let alertRuleOptionList = modal.alertRule.optionList;

            scoreOptionList.forEach(function(item,index){
                let itemOid = item.oid;
                scoreSelectList.forEach(function(selectOid){
                    if(selectOid === itemOid){
                        item.state = 'waiting';
                        self.doDetect(customerOid,itemOid,'socre')
                            .then(function(res){
                                item.state = 'success';
                                item.desc = res.desc;
                                modal.ticktock.call(self);
                            },function(res){
                                item.state = 'error';
                                item.desc = res.desc;
                                modal.ticktock.call(self);
                            }).catch(function(res){
                                console.error(res)
                                item.desc = res.desc;
                                item.state = 'error';
                            });
                    }
                });
            });

            alertRuleOptionList.forEach(function(item,index){
                let itemOid = item.oid;
                alertRuleSelectList.forEach(function(selectOid){
                    if(selectOid === itemOid){
                        item.state = 'waiting';
                        self.doDetect(customerOid,itemOid,'alertRule')
                            .then(function(res){
                                item.state = 'success';
                                modal.ticktock.call(self);
                            },function(res){
                                item.state = 'error';
                                modal.ticktock.call(self);
                            }).catch(function(res){
                                item.state = 'error';
                            });
                    }
                });
            });
        },
        doDetect: function(customerOid,id,type){
            let self = this;
            let urlType = (type === 'socre') ? '/scoreCards/' : '/alertRules/';
            let url = '/app/v1/tool/run/customers/' 
                        + customerOid 
                        + urlType 
                        + id;

            return new Promise(function(resolve,reject){
                self.$http.get(url)
                    .then(function(res){
                        if(res.code === 0){
                            resolve(res);
                        }else{
                            reject(res);
                        }
                    }).catch(function(res){
                        reject(res);
                    });
            });
        },
        loadScoreCardList: function(){
            let vm = this;
            var url = "/app/v1/riskTool/scoreCards";
            vm.$http.get(url, {
                        params : {
                            pageSize: 1000,
                            toolObjectCode: 'customer',
                            enable: true,
                            status: 1
                        }
                    })
                    .then(function(res) {
                        if(res.code === 0){
                            let respData = res.data.data;
                            respData.forEach(function(item,index){
                                item.state = 'default';
                                vm.RunDetectModal.score.optionList.push(item);
                            });
                        }else{
                            vm.$Message(res.desc);
                        }
                    });
        },
        loadAlertRuleList: function(){
            let vm = this;
            let url = "/app/v1/riskTool/alertRules";
            vm.$http.get(url, {
                        params : {
                            pageSize: 10000,
                            toolObjectCode: 'customer',
                            enable: true
                        }
                    })
                    .then(function(res) {
                        if(res.code === 0){
                            let respData = res.data.data;
                            respData.forEach(function(item,index){
                                item.state = 'default';
                                vm.RunDetectModal.alertRule.optionList.push(item);
                            });
                        }else{
                            vm.$Message(res.desc);
                        }
                    });
        }
    }
};
</script>
